import Vue from 'vue'
import Router from 'vue-router'
import TopNav from '../components/nav/topNav.vue'
import LeftNav from '../components/nav/leftNav.vue'
import Home from '../views/home.vue'
import Dashboard from '../views/home/dashboard.vue'
import HotboomLin from '../views/product/hotboomLin.vue'
import Distribution from '../views/product/distributionLin.vue'
import Library from '../views/product/distribution/library.vue'
import DataCollection from '../views/product/hotboom/dataCollection.vue'
import DataLibrary from '../views/product/hotboom/dataLibrary.vue'
import PublishPool from '../views/published/publishPool.vue'
import Skuset from '../views/published/skushop.vue'
import Inentory from '../views/commodity/inventory.vue'
import OrderList from '../views/order/orderList.vue'
import Longistics from '../views/order/logistics.vue'
import Syncab from '../views/order/syncab.vue'
import AccountChecking from '../views/finance/accountChecking.vue'
import Shopauthorization from '../views/system/Shopauthorization.vue'
import Notfound from '../views/404.vue'

// 懒加载方式，当路由被访问的时候才加载对应组件
const Login = (resolve) => require(['../views/Login.vue'], resolve)
Vue.use(Router)
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
	return originalPush.call(this, location).catch((err) => err)
}
let router = new Router({
	routes: [
		{
			path: '/login',
			type: 'login',
			component: Login,
		},
		{
			path: '*',
			component: Notfound,
		},
		{
			path: '/index',
			type: 'home',
			name: 'home',
			redirect: '/dashboard',
			component: Home,
			children: [
				{
					path: '/dashboard',
					name: 'index',
					alias: '首页',
					components: {
						default: Dashboard,
						top: TopNav,
						//   aside: LeftNav
					},
					leaf: true, // 只有一个节点
					iconCls: '', // 图标样式class
					menuShow: true,
				},
			],
		},
		{
			path: '/productManager',
			type: 'product',
			name: 'product',
			component: Home,
			redirect: '/authManage',
			menuShow: true,
			children: [
				{
					path: '/authManage',
					alias: '代购',
					name: 'authManage',
					components: {
						default: HotboomLin,
						top: TopNav,
						aside: LeftNav,
					},
					leaf: false,
					iconCls: 'el-icon-goods',
					menuShow: true,
					children: [
						{
							path: '/dataManage',
							component: DataCollection,
							name: 'dataManage',
							alias: '数据采集',
							menuShow: true,
						},
						{
							path: '/dataLibraryManage',
							component: DataLibrary,
							name: 'dataLibraryManage',
							alias: '产品库',
							menuShow: true,
						},
					],
				},
				{
					path: '/roleManage',
					name: 'roleManage',
					alias: '分销',
					components: {
						default: Distribution,
						top: TopNav,
						aside: LeftNav,
					},
					// leaf: false,
					iconCls: 'el-icon-setting',
					menuShow: true,
					children: [
						{
							path: '/library',
							component: Library,
							name: 'roleManagelib',
							alias: '分销商品库',
							menuShow: true,
						},
					],
				},
			],
		},
		{
			path: '/pubilshedManage',
			type: 'published',
			name: 'published',
			component: Home,
			redirect: '/publishPool',
			menuShow: false,
			children: [
				{
					path: '/publishPool',
					name: 'accountManage',
					alias: '产品刊登池',
					components: {
						default: PublishPool,
						top: TopNav,
						aside: LeftNav,
					},
					leaf: true,
					iconCls: 'el-icon-news',
					menuShow: true,
				},
				{
					path: '/integralManage',
					name: 'lazaManage',
					alias: 'lazada刊登',
					components: {
						default: Skuset,
						top: TopNav,
						aside: LeftNav,
					},
					leaf: true,
					iconCls: 'el-icon-news',
					menuShow: true,
				},
			],
		},
		{
			path: '/contentManage',
			type: 'commodity',
			name: 'commodity',
			component: Home,
			redirect: '/Inentory',
			menuShow: false,
			children: [
				{
					path: '/Inentory',
					name: 'shopManage',
					alias: '库存SKU',
					components: {
						default: Inentory,
						top: TopNav,
						aside: LeftNav,
					},
					leaf: true,
					iconCls: 'el-icon-printer',
					menuShow: true,
				},
			],
		},
		{
			path: '/orderManage',
			type: 'order',
			name: 'order',
			component: Home,
			redirect: '/OrderList',
			menuShow: false,
			children: [
				{
					path: '/OrderList',
					name: 'orderlistMange',
					alias: '订单列表',
					components: {
						default: OrderList,
						top: TopNav,
						aside: LeftNav,
					},
					leaf: true,
					iconCls: 'el-icon-printer',
					menuShow: true,
				},
				{
					path: '/syncab',
					name: 'syncMange',
					alias: '同步异常',
					components: {
						default: Syncab,
						top: TopNav,
						aside: LeftNav,
					},
					leaf: true,
					iconCls: 'el-icon-printer',
					menuShow: true,
				},
				{
					path: '/logistics',
					name: 'logisticsMange',
					alias: '物流授权',
					components: {
						default: Longistics,
						top: TopNav,
						aside: LeftNav,
					},
					leaf: true,
					iconCls: 'el-icon-printer',
					menuShow: true,
				},
			],
		},
		{
			path: '/financeManage',
			type: 'finance',
			name: 'finance',
			component: Home,
			redirect: '/Inentory',
			menuShow: false,
			children: [
				{
					path: '/AccountChecking',
					name: 'financereconcileMange',
					alias: '财务对账',
					components: {
						default: AccountChecking,
						top: TopNav,
						aside: LeftNav,
					},
					leaf: true,
					iconCls: 'el-icon-printer',
					menuShow: true,
				},
			],
		},
		{
			path: '/systemManage',
			type: 'system',
			name: 'system',
			component: Home,
			redirect: '/Shopauthorization',
			menuShow: false,
			children: [
				{
					path: '/Shopauthorization',
					name: 'ShopauthorizationMange',
					alias: '店铺授权',
					components: {
						default: Shopauthorization,
						top: TopNav,
						aside: LeftNav,
					},
					leaf: true,
					iconCls: 'el-icon-printer',
					menuShow: true,
				},
			],
		},
	],
})

// const router = new VueRouter({
// 	mode: 'history',
// 	base: process.env.BASE_URL,
// 	routes,
// })
router.beforeEach((to, from, next) => {
	if (to.path.startsWith('/login')) {
		window.localStorage.removeItem('access-user')
		next()
	} else {
		//   let user = JSON.parse(window.localStorage.getItem('mabangerp_token'))
		let user = window.localStorage.getItem('mabangerp_token')
		if (!user) {
			next({ path: '/login' })
		} else {
			next()
		}
		// next()
	}
})
export default router
